﻿using System;
using System.Data;
using System.Data.SqlClient;
using System.Collections.Generic;
using System.Text;
using Portal.Environment;
using Portal.Language;
using Portal.Interface;

namespace Portal.CMS
{
    public class AddAlbum : WebPart
    {
        public override string WebPartId
        {
            get
            {
                return "AddAlbum";
            }
        }

        public override string Title
        {
            get
            {
                return ResourceBLL.GetText(WebPartId, "Title", "Thêm mới Album");
            }
        }

        public override string Description
        {
            get
            {
                return ResourceBLL.GetText(WebPartId,"Description","");
            }
        }


        public override string RenderWebPart(LoadParamCls loadParamCls)
        {
            AjaxPro.Utility.RegisterTypeForAjax(typeof(AddAlbum));

            string SelectAlbumTypeText =
                "<select id=drpSelectAlbumType>\r\n" +
                "   <option value=\"\">=======</option>"+
                "   <option value=\"picture\">Kiểu ảnh</option>" +
                "   <option value=\"book\">Kiểu sách</option>" +
                "</select>\r\n";
            string SelectLanguageText =
              "<select id=divSelectLanguage>";
            string[] Languages = Environment.Environments.GetLanguages();
            for (int iIndex = 0; iIndex < Languages.Length; iIndex++)
            {
                if (Environment.Environments.ActiveLanguage.Equals(Languages[iIndex]))
                {
                    SelectLanguageText += "<option selected value=\"" + Languages[iIndex] + "\">" + Languages[iIndex] + "</option>";
                }
                else
                {
                    SelectLanguageText += "<option value=\"" + Languages[iIndex] + "\">" + Languages[iIndex] + "</option>";
                }
            }
            SelectLanguageText += "</select>";
            string belongSite = (string)Environment.Environments.Request("belongSite");
            string RetHtml =
                WebPartUtilities.AttachJavascript("Portal.CMS", "Album.js") +
                         Environment.Environments.GetFormDialog(
                              "<input id=txtBelongSite type=hidden value=\"" + belongSite + "\">" +
                             "      <table>" +
                             "          <tr>" +
                             "              <td>" + ResourceBLL.GetText(WebPartId, "AddAlbumLanguage", "Ngôn ngữ") + "</td>" +
                             "              <td>" + SelectLanguageText + "</td>" +
                             "          </tr>" +
                             "          <tr>" +
                             "              <td>" + ResourceBLL.GetText(WebPartId, "AddAlbumCaption", "Nhóm") + "</td>" +
                             "              <td><input class=cssTextBoxDialog id=txtAlbum></td>" +
                             "          </tr>" +

                             "          <tr>" +
                             "              <td>" + ResourceBLL.GetText(WebPartId, "Type", "Kiểu") + "</td>" +
                             "              <td>"+SelectAlbumTypeText+"</td>" +
                             "          </tr>" +

                             "          <tr>" +
                             "              <td></td>" +
                             "              <td><input type=button class=cssSaveButton onclick=javascript:ClientSaveAddAlbum(); value=\"" + ResourceBLL.GetText(WebPartId, "AddAlbumSaveButton", "Ghi lại") + "\"><input type=button class=cssCancelButton onclick=javascript:window.close(); value=\"" + ResourceBLL.GetText(WebPartId, "AddAlbumCancelButton", "Thoát") + "\"></td>" +
                             "          </tr>" +
                             "      </table>", ResourceBLL.GetText(WebPartId, "AddAlbumFormTitle", "Thêm mới Album"));
            return RetHtml;
        }

        [AjaxPro.AjaxMethod]
        public string ServerSideAddAlbum(
            string belongSite,
            string ActiveLanguage,
            string AlbumName,
            string AlbumType)
        {
            try
            {
                Environment.Environments.CheckSessionTimeOut(true);
                if (AlbumName.Trim().Length == 0) throw new Exception(ResourceBLL.GetText(WebPartId,"EnterName","Nhập tên nhóm"));
                string AlbumId = new AlbumBLL().AddAlbum(null, belongSite, ActiveLanguage, AlbumName, AlbumType);
                return ResourceBLL.GetText(WebPartId,"MessageAddOk","Thêm thành công!");
            }
            catch (Exception ex)
            {
                return "ERROR: " + ex.Message.ToString();
            }
        }
    }
}